﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace Helper
{
    public class  LogHandler
    {
       string logFile=string.Empty;

       private LogHandler()
        {
            Initialize();
        }

        public static readonly LogHandler Instance = new LogHandler();

        public void Initialize()
        {
            try
            {
                 logFile = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\" + System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath) + ".log";

            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

        public void VTLogInfo(string aMessage)
        {
            VTLog("[INFO] " + aMessage);
        }

        public void VTLogWarning(string aMessage)
        {
            VTLog("[WARNING] " + aMessage);
        }

        public void VTLogError(string aMessage)
        {
            VTLog("[ERROR] " + aMessage);
        }
        
        /// <summary>
        /// 追加一行信息
        /// </summary>
        /// <param name="text"></param>
        public void VTLog(string text)
        {
            text += "\r\n";
            using (FileStream fs = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
                    sw.Close();
                }
                fs.Close();
            }
        }
    }
}
